#include <iostream>
#include <vector>
#include <string>
#include <sw/redis++/redis++.h>
#include "until.hpp"
#include <thread>
using namespace std;
using namespace chrono_literals;




void test1(sw::redis::Redis& redis)
{
    cout << "test hset hget hdel" << endl;
    redis.flushall();
    //1.正常
    redis.hset("key1","a","1");
    //2.initializer_list
    redis.hset("key1",{"b","2"});
    //3.插入pair
    const std::pair<sw::redis::StringView, sw::redis::StringView> item = make_pair("c","3");
    redis.hset("key1",item);
    //4.插入容器中元素
    vector<pair<string,string>> vpss{
        make_pair("d","4"),
        make_pair("e","5")
    };
    redis.hset("key1",vpss.begin(),vpss.end());

    sw::redis::OptionalString os = redis.hget("key1","a");
    if(os)
        cout << os.value() << endl;
    else 
        cout << "无效元素" << endl;
    
    auto ret = redis.hdel("key1","a");
    cout << ret << endl;

    os = redis.hget("key1","a");
    if(os)
        cout << os.value() << endl;
    else 
        cout << "无效元素" << endl;
}

void test2(sw::redis::Redis& redis)
{
    cout << "test hexists hvals" << endl;
    redis.flushall();

    vector<pair<string,string>> vpss{
        make_pair("a","1"),
        make_pair("b","2"),
        make_pair("c","3")

    };
    redis.hset("key1",vpss.begin(),vpss.end());

    bool ret = redis.hexists("key1","a");
    cout << ret << endl;

    vector<string> vs;
    back_insert_iterator<vector<string>> bit(vs);
    redis.hvals("key1",bit);
    PrintElenums(vs);
}

void test3(sw::redis::Redis& redis)
{
    cout << "test hlen hgetall" << endl;
    redis.flushall();

    vector<pair<string,string>> vpss{
        make_pair("a","1"),
        make_pair("b","2"),
        make_pair("c","3")
    };
    redis.hset("key1",vpss.begin(),vpss.end());

    auto sum = redis.hlen("key1");
    cout << sum << endl;

    vector<pair<string,string>> ret;
    back_insert_iterator<vector<pair<string,string>>> bit(ret);

    redis.hgetall("key1",bit);
    PrintElenums(ret);
}

int main()
{
    sw::redis::Redis redistmp("tcp://127.0.0.1:6379");
    // test1(redistmp);
    // test2(redistmp);
    test3(redistmp);
    return 0;
}